<?php
include_once("../../service/receiptService.php");
include_once("../../service/productService.php");
include_once("../../service/userService.php");

$recId = $_POST["recId"];
$receipt = mysql_fetch_array(findReceiptBy($recId));
?>
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <button type="button" class="close print-receipt" onclick="$('#form').attr('target', '_blank');submitForm('form', './print-receipt.php?id=<?php echo $recId;?>', 'post');">In hóa đơn</button>
    Chi tiết hóa đơn
</div>
<div class="modal-body" id="receipt-info">
	<table border="1" cellpadding="0" cellspacing="0">
    	<tr>
        	<td>Khách hàng</td>
            <td>
				<?php
					$customer = mysql_fetch_array(findUserById($receipt["USER_ID"]));
					echo $customer["USER_EMAIL"]." - ".$customer["USER_USERNAME"];
				?>
            </td>
        </tr>
    	<tr>
        	<td>Mã</td>
            <td><?php echo $receipt["REC_CODE"];?></td>
        </tr>
        <tr>
        	<td>Ngày tạo</td>
            <td>
				<?php 
					$date = new DateTime($receipt["REC_DATE"]);
					echo $date->format('d/m/Y');
				?>
            </td>
        </tr>
        <tr>
        	<td>Trạng thái</td>
            <td>
				<?php 
					$statuses = findAllStatus();
					$status = $receipt["REC_STATUS"];
					echo $statuses[$status];
				?>
            </td>
        </tr>
        <tr>
        	<td>Địa chỉ giao hàng</td>
            <td><?php echo $receipt["REC_DELIVERYINGADDRESS"];?></td>
        </tr>
        <tr>
        	<td>Địa chỉ trả tiền</td>
            <td><?php echo $receipt["REC_PAYINGADDRESS"];?></td>
        </tr>
        <tr>
        	<td>Ngày giao hàng</td>
            <td>
				<?php 
					$deliveryingDate = new DateTime($receipt["REC_DELIVERYINGDATE"]);
					echo $deliveryingDate->format('d/m/Y');
				?>
            </td>
        </tr>
        <tr>
        	<td>Ngày thật sự giao hàng</td>
            <td>
				<?php 
					if($receipt["REC_ACTUALLYDELIVERYINGDATE"] != ""){
						$actuallyDeliveryingDate = new DateTime($receipt["REC_ACTUALLYDELIVERYINGDATE"]);
						echo $actuallyDeliveryingDate->format('d/m/Y');
					}
				?>
            </td>
        </tr>
        <tr>
        	<td>Số điện thoại</td>
            <td><?php echo $receipt["REC_PHONENUMBER"];?></td>
        </tr>
        <tr>
        	<td>Ghi chú</td>
            <td><?php echo $receipt["REC_NOTE"];?></td>
        </tr>
        <tr>
            <td colspan="2">
            	<table border="1" cellpadding="0" cellspacing="0" frame="void">
                	<thead>
                    	<th>STT</th>
                        <th>Mã</th>
                    	<th>Sản phẩm</th>
                        <th>Số lượng</th>
                        <th>Giá</th>
                        <th>Lựa chọn</th>
                        <th>Ghi chú</th>
                    </thead>
                    <tbody>
                    <?php
						$receiptItems = findReceiptItemBy($recId);
						$count = 1;
						while($row = mysql_fetch_array($receiptItems)){
                    ?>
                    	
                            <tr>
                            	<td><?php echo $count++; ?></td>
                                <td>
									<?php
										$image = mysql_fetch_array(findLimitedImagesOfProduct($row["PRO_ID"], 1));
                                        echo "<img src='../image/product/".$row["PRO_ID"]."/".$image["IMG_PATH"]."' width='50' height='50'/>";
                                	?>
                                </td>
                                <td>
                                    <?php
                                        $product = findProductBy($row["PRO_ID"]);
                                        echo $product["PRO_NAME"]." (".$product["PRO_CODE"].")";
                                    ?></td>
                                <td><?php echo $row["REI_QUANTITY"];?></td>
                                <td><?php echo number_format($row["REI_PRICE"]);?></td>
                                <td>
                                    <?php
                                        $optionItems = findReceiptProductOptionItemBy($row["PRO_ID"], $row["REC_ID"]);
                                        
                                        while($optionItem = mysql_fetch_array($optionItems)){
                                            echo "- ".$optionItem["OPTIONITEM"]."<br/>";
                                        }
                                    ?>
                                </td>
                                <td><?php echo $row["REI_NOTE"]; ?></td>
                            </tr>
                    <?php
						}
                    ?>
                    </tbody>
                </table>
			</td>
        </tr>
    </table>
</div>
<div class="modal-footer"></div>
<script>
function openWindow(content){
	top.consoleRef=window.open('','myconsole', 'width=800,height=600,menubar=0,toolbar=1,status=0,scrollbars=1,resizable=1');
		top.consoleRef.document.writeln(
	  '<html><head><title>Console</title></head>'
	   +'<body bgcolor=white onLoad="self.focus()">'
	   +content
	   +'</body></html>')
 	top.consoleRef.document.close();
}
</script>
<?php
?>